﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Classwork03
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double side = Convert.ToDouble(textBox1.Text);
                Graph g = new Square(side, "Square");
                if (g.IsLegal())
                    label2.Text = g.ToString();
                else
                    label2.Text = "非法输入！";
            }
            catch (System.FormatException)
            {
                label2.Text = "非法输入！";
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                double length = Convert.ToDouble(textBox2.Text);
                double width = Convert.ToDouble(textBox3.Text);
                Graph g = new Rectangle(length, width, "Rectangle");
                if (g.IsLegal())
                    label3.Text = g.ToString();
                else
                    label3.Text = "非法输入！";
            }
            catch (System.FormatException)
            {
                label3.Text = "非法输入！";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                double radius = Convert.ToDouble(textBox4.Text);
                Graph g = new Circle(radius, "Circle");
                if (g.IsLegal())
                    label4.Text = g.ToString();
                else
                    label4.Text = "非法输入！";
            }
            catch (System.FormatException)
            {
                label4.Text = "非法输入！";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            Graph[] g = new Graph[10];
            string output1 = "生成了：";
            string output2 = "总面积为：";
            
            double rndlength = 10 * rnd.NextDouble();

            for (int i = 0; i < 10; i++)
            {
                int rndnumber = rnd.Next(3);
                switch (rndnumber)
                {
                    case 0:
                        {
                            g[i] = new Square(10 * rnd.NextDouble(), "Square");
                            output1 += "正方形 ";
                            break;
                        }
                    case 1:
                        {
                            g[i] = new Rectangle(10 * rnd.NextDouble(), 10 * rnd.NextDouble(), "Rectangle");
                            output1 += "长方形 ";
                            break;
                        }
                    case 2:
                        {
                            g[i] = new Circle(10 * rnd.NextDouble(), "Circle");
                            output1 += "圆形 ";
                            break;
                        }
                }
            }

            double area = 0;
            foreach(Graph i in g)
            {
                area += i.CalculateArea();
            }
            output2 += area;

            label5.Text = output1;
            label10.Text = output2;
        }

    }
}
